<%@page import="DB.DBProvider"%>
<%@ page language="java" contentType="text/html; charset=windows-1255"
    pageEncoding="windows-1255"%>
<%@ page import="java.util.*, java.io.*,java.sql.ResultSet" %>
<% 
   String user = (String)request.getSession().getAttribute("username");
   String admin = (String)request.getSession().getAttribute("administrator");
   Properties p = new Properties();
   p.load(getServletContext().getResourceAsStream("/WEB-INF/config"));
   ResultSet res = (new DBProvider(p)).getCoursesUser(user);
	 
%>	
<script src="validate.js" type="text/javascript"></script>
<script>
	$(document).ready(function()
		{	
		
			$('#addcourse1').click(function(){
					
				$('#content').load("/Calendars/Homeservlet?op=addcourse",function(response, status, xhr) {
					  if (status == "error") {
						    var msg = "Sorry but there was an error: ";
						    $("#page").html("<br />" +"<h1>" + msg + xhr.status + " " + xhr.statusText +"</h1>" );
						  }
						});});
			
			$('#Browse1').click(function(){
				$('#content').load("/Calendars/Homeservlet?op=courseSearch",function(response, status, xhr) {
					  if (status == "error") {
						    var msg = "Sorry but there was an error: ";
						    $("#page").html("<br />" +"<h1>" + msg + xhr.status + " " + xhr.statusText +"</h1>" );
						  }
						});});
			
			$('#you1').click(function(){
				$('#page').load("/Calendars/Homeservlet?op=timeTable",function(response, status, xhr) {
					  if (status == "error") {
						    var msg = "Sorry but there was an error: ";
						    $("#page").html("<br />" +"<h1>" + msg + xhr.status + " " + xhr.statusText +"</h1>" );
						  }
				});});

			
			

		});
</script>

<script type="text/javascript">


	function remove_course1(title)
	{
		var t;
		t = encodeURIComponent(title);
		$('#content').load("/Calendars/Homeservlet?op=unregister&title=" + t,function(response, status, xhr) {
			  if (status == "error") {
				    var msg = "Sorry but there was an error: ";
				    $("#page").html("<br />" +"<h1>" + msg + xhr.status + " " + xhr.statusText +"</h1>" );
				  }
				});
		return true;
	}
	function coursepage1(title)
	{
		var t;
		t = encodeURIComponent(title);
		$('#content').load("/Calendars/Homeservlet?op=coursespage&title=" + t,function(response, status, xhr) {
			  if (status == "error") {
				    var msg = "Sorry but there was an error: ";
				    $("#page").html("<br />" +"<h1>" + msg + xhr.status + " " + xhr.statusText +"</h1>" );
				  }
				});
		return true;
	}
	
</script>
	
<!-- content -->
<div id="content" class="post"">
	<h1 class="title">Hello <%=user%>, here is your courses</h1>
		


	
<%   
   if (!res.next())
   {
   
%>
	  
   
	<h1> There aren't any courses to display</h1>
	   
<%  } else 
	{
	res.beforeFirst();
	%>
			<br/>
			<table class = "usersTable">
				<tr>
				<th>Title</th>
				<th>Description</th>
				<th>Credit points</th>
				<th>Capacity</th>
				<th>group number</th>
				<th></th>
    			<th></th>
			</tr>
<%	
		while (res.next())
		{
				String title = res.getString("course_title");
				String description = res.getString("description");
				description = description.substring(0,Math.min(50,description.length()));
				description += "...";
				String credit_points = Integer.toString(res.getInt("number_credit_points"));
				String capacity = Integer.toString(res.getInt("course_capacity"));
				String group_number = Integer.toString(res.getInt("group_number"));
%>			
		
				<tr>
					<td><%=title%></td>
					<td><%=description%></td>
					<td><%=credit_points%></td>
					<td><%=capacity%></td>
					<td><%=group_number%></td>
					<td><button onclick="coursepage1(<%="'" + title + "'"%>)">Course Page</button></td>
    				<td><button onclick="remove_course1(<%="'" + title + "'" %>)">Unregister course</button></td>

				</tr>
<%
			}
		}
			
%>
			</table>






	</div>
	
	<!-- sidebar -->
	<div id="sidebar">
		<ul>
		<%//only admin can add courses
		  if (admin.equals("true")) {
  		 %>
			<li><h2>Add Course</h2>
				<center>
					<table>
						<tr>
							<td><button id="addcourse1">Click Here</button></td>
						</tr>
					</table>
				</center>
			</li>
			<%
		 		 }
			%>
			<li><h2>Your Time Table</h2>
				<center> 
					<table>
						<tr>
							<td><button id="you1">Click Here</button></td>
						</tr>
					</table>
				</center> 
			</li>
			<li><h2>Browse and search</h2>
				<center> 
					<table>
						<tr>
							<td><button id="Browse1">Click Here</button></td>
						</tr>
					</table>
				</center> 
			</li>
		</ul>
	</div>			
